require('GameApiFix')('1.0.6.0');

window.GLOBAL = {
    TOTAL_TIME: 10,
    TOTAL_MOVE: 10,
    FACTOR_TIME: 12 / 10,
    FACTOR_MOVE: 1000,
    FACTOR_MOVE_MIN: 100,

    STATE_STILL: 0,
    STATE_MOVE: 1,
    STATE_ACCEL: 2,
    STATE_JUMP: 3,
    STATE_SPRINT: 4,
    STATE_DECEL: 5,
    STATE_FALL_HEAD: 6,
    STATE_FALL_BACK: 7,
    STATE_DECEL_MOVE: 8,

    WIN_WIDTH: 0,
    WIN_HEIGHT: 0,

    IS_NEW_PLAYER: true,
    IS_MOVE_END: true,

    motorsInfo: null,
    _gameState: null,
    gameResult: null,
    playerResult: null,

    set gameState(data) {
        var bikes = data.bikes;
        var allRate = 0;
        for(var i in bikes) allRate += bikes[i].rate;
        for(var i in bikes) {
            var b = bikes[i];
            var s = 61.2 / b.rate + b.random * 5 + 18.8 + allRate / 5;
            b.status = Math.min(s, 94) / 100;
        }
        this._gameState = data;
    },

    get gameState() {
        return this._gameState;
    },

    getMotorEnergy(order) {
        var rdm = this.getMotorStatus(order).random * 5;
        var info = this.getMotorInfo(order);
        var phase = (this._gameState.gameId + order) % 5;
        var factor = [-0.3, -0.12, 0, 0.12, 0.3];
        var getOrder = (i) => { 
            var ret = phase + i;
            return ret > 4 ? ret - 5 : ret; 
        };
        return {
            zouxian: info.zouxian + rdm * factor[getOrder(0)],
            bizhen: info.bizhen + rdm * factor[getOrder(1)],
            dongli: info.dongli + rdm * factor[getOrder(2)],
            zhidong: info.zhidong + rdm * factor[getOrder(3)],
            gonglv: info.gonglv + rdm * factor[getOrder(4)]
        };
    },

    getMotorStatus(order) {
        var bikes = this._gameState.bikes;
        for(var i in bikes) {
            var d = bikes[i];
            if(d.id == order + 1) return d;
        }
    },

    getMotorInfo(order) {
        for(var i in this.motorsInfo) {
            var d = this.motorsInfo[i];
            if(d.id == order + 1) return d;
        }
    },
    
    getSecondsToAnyHour(hour) {
        var date = new Date();
        var sec = parseInt(date.getTime() % 86400000 / 1000);
        var h = hour - 8;
        if (sec <= h * 3600) {
            return h * 3600 - sec;
        } else {
            return 86400 + h * 3600 - sec;
        }
    },
}

window.URLS = {
    FirstLoad: CONFIG.baseUrl + '/api_platform/api/uic/buryingPoint/firstLoad',
    UserInfo: CONFIG.baseUrl + '/api_platform/api/user/transInfo',
    MotorsInfo: CONFIG.baseUrl + '/api_bike/api/game/bikeInfo',
    
    GameState: CONFIG.baseUrl + '/api_bike/api/game/status',
    GameResult: CONFIG.baseUrl + '/api_bike/api/game/result',
    PlayerResult: CONFIG.baseUrl + '/api_bike/api/game/result/me',

    PlayerBet: CONFIG.baseUrl + '/api_bike/api/game/betting',
    CancelBet: CONFIG.baseUrl + '/api_bike/api/game/cancel',

    BettingRate: CONFIG.baseUrl + '/api_bike/api/game/bettingRate',
    MotorsHistory: CONFIG.baseUrl + '/api_bike/api/history/win',
    PlayerHistory: CONFIG.baseUrl + '/api_bike/api/history/me',
    PlayerHistoryDetail: CONFIG.baseUrl + '/api_bike/api/history/gameDetail',

    AwardRankState: CONFIG.baseUrl + '/api_ops/api/ops/ranking/status',
    AwardRankMine: CONFIG.baseUrl + '/api_ops/api/ops/ranking/myRecord',
    AwardRankToday: CONFIG.baseUrl + '/api_ops/api/ops/ranking/bettingRank',
    AwardRankFlag: CONFIG.baseUrl + '/api_ops/api/ops/ranking/awardsFlag',

    PacketState: CONFIG.baseUrl + '/api_ops/api/luckey/packets/status',
}